﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace InsideDL.MISData
{
    public class DataIG004
    {
        /// <summary>
        /// 取出所有的Insurance Company
        /// </summary>
        /// <param name="p_cos"></param>
        /// <returns></returns>
        public string[] GetInsuranceCO(object[,] p_cos)
        {
            //string[] q = (from x in p_cos.Cast<string>() where x != null 
            //              select x.ToString()).ToArray<string>();
            //IEnumerable<string> aaa = p_cos.Cast<string>();
            //return q.Cast<string>().ToArray<string>() ;

            var q = from x in p_cos.Cast<string>() where x != null select x;
            return q.ToArray<string>();
        }

        public string[] GetInsuranceCO(object[,] p_cos, string p)
        {
            List<string> co = new List<string>();
            for (int x = 1; x <= p_cos.GetLength(0); x++)
            {
                for (int y = 1; y <= p_cos.GetLength(1); y++)
                {
                    if (p_cos.GetValue(x, y) != null)
                    {
                        co.Add(p_cos.GetValue(x, y).ToString());
                    }
                    
                }
            }
            return co.ToArray<string>();
        }



        /// <summary>
        /// 取回保費收入
        /// </summary>
        /// <param name="p_income"></param>
        /// <returns></returns>
        public DataTable GetInsuranceIncome(object[,] p_income)
        {
            DataTable dt = null;
            if (p_income.GetValue(1,1) != null)
            {
                dt = getTable();
                for (int x = 1; x <= p_income.GetLength(0); x++)
                {
                    for (int y = 2; y <= p_income.GetLength(1); y += 2)
                    {
                        //1 2 4 6 8
                        DataRow row = dt.NewRow();
                        row[0] = p_income.GetValue(x, 1) == null ? "業界合計"
                            : p_income.GetValue(x, 1).ToString();
                        row[1] = p_income.GetValue(x, y).ToString();
                        dt.Rows.Add(row);
                    }
                }            
            }
            return dt;
        }

        /// <summary>
        /// 取得通路資料
        /// </summary>
        /// <param name="p_route"></param>
        /// <param name="p_pos">通路資料陣列中的位置</param>
        /// <returns></returns>
        public DataTable GetRoute(object[,] p_route, int[] p_pos)
        {
            DataTable dt = getTable();
            for (int x = 1; x <= p_route.GetLength(0); x++)
            {
                foreach (int y in p_pos)
                {
                    DataRow row = dt.NewRow();
                    row[0] = p_route.GetValue(x, 1);
                    row[1] = p_route.GetValue(x, y) == null ? string.Empty
                        : p_route.GetValue(x, y).ToString();
                    dt.Rows.Add(row);
                }
            }
            return dt;
        }


        protected DataTable getTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("fld1", typeof(System.String));
            dt.Columns.Add("fld2", typeof(System.String));
            return dt;
        }

        public IQueryable<DataRow> GetSeleIncome(string p_co,DataTable p_dt)
        {
            IQueryable<DataRow> query = (from acc in p_dt.AsEnumerable()
                                         where acc.Field<string>("fld1").ToString().Equals(p_co)
                                         select acc).AsQueryable();
            return query;
        }

    }
}
